package com.gxc.aop;

import com.gxc.annotation.Action;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 切面类.
 *
 * @author 宫新程
 * @since 2018/12/14 11:16 AM
 */
@Aspect
@Component
public class LogAspect {

  /** 注解声明切点. */
  @Pointcut("@annotation(com.gxc.annotation.Action)")
  public void annotationPointCut() {}

  /** 注解式：After. */
  @After("LogAspect.annotationPointCut()")
  public void after(JoinPoint joinPoint) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    Action action = method.getAnnotation(Action.class);
    System.out.println("注解式拦截：" + action.name());
  }

  /** 方法式：Before. */
  @Before("execution(* com.gxc.aop.MethodService.*(..))")
  public void before(JoinPoint joinPoint) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    System.out.println("方法规则拦截：" + method.getName());
  }
}
